package sz.szsmk.citizencard.mvp.presenter;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.SearchViewRequest;
import sz.szsmk.citizencard.model.network.bean.response.SearchViewResponse;
import sz.szsmk.citizencard.mvp.contract.ISearchContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;

import javax.inject.Inject;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/13 11:39
 * description:
 */
public class SearchPresenter extends IBasePresenter
        implements ISearchContact.ISearchPresenter<ISearchContact.ISearchView> {


    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public SearchPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void getSearchData(String keyWord) {
        SearchViewRequest searchViewRequest = new SearchViewRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        searchViewRequest.setAccess_token(access_token);
        searchViewRequest.setGroup_type(2);
        searchViewRequest.setKeyword(keyWord);
        mNetworkDateSource.searchSpotAndHotelByKeyword(searchViewRequest, new SubscribeResult<SearchViewResponse>() {
            @Override
            protected void onOk(SearchViewResponse response) {
                if (mView != null) {
                    ((ISearchContact.ISearchView) mView).updataSuccess(response);
                }
            }

            @Override
            protected void onFailure(SearchViewResponse response) {
                super.onFailure(response);
                ((ISearchContact.ISearchView) mView).updataError(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((ISearchContact.ISearchView) mView).updataError(throwable.getMessage());

            }
        });

    }
}
